package models

import (
	"github.com/beego/beego/v2/client/orm"
	"time"
	"weiboxs.com/new_h_glasses/utils"
)

type InspectsSum struct {
	Id           int64
	DepartmentId int64
	Archives     *Archives `orm:"rel(fk)"`
	ProNum       int     //产品数量
	PaymentType  int     //支付状态1已付2未付
	TotalMoney   float64 //总金额
	ChangesMoney float64 //调减金额
	SumPayable   float64 //应付金额
	AlipayMoney  float64 //支付宝
	WechatMoney  float64 //微信支付
	CardMoney    float64 //刷卡支付
	CashMoney    float64 //现金
	CreatedAt    int64
	UpdatedAt    int64
	CreatedBy    string
	UpdatedBy    string
}

//查询
type InspectsSumQueryParam struct {
	BaseQueryParam
	ArchivesId int64
}

func (a *InspectsSum) TableName() string {
	return InspectsSumTableName()
}

func InspectsSumTableName() string {
	return TableName("sa_inspects_sum")
}

//查询分页数据
func InspectsSumPageList(params *InspectsSumQueryParam) ([]*InspectsSum, int64) {
	query := orm.NewOrm().QueryTable(InspectsSumTableName())
	data := make([]*InspectsSum, 0)
	sortorder := "-Id"

	switch params.Sort {
	case "Id":
		sortorder = "Id"
	}
	if params.Order == "desc" {
		sortorder = "-" + sortorder
	}

	if params.ArchivesId > 0 {
		query = query.Filter("archives_id", params.ArchivesId)
	}

	query = query.Filter("department_id", CurLoginUserInfo.DepartmentId)

	total, _ := query.Count()

	query.OrderBy(sortorder).Limit(params.Limit, params.Offset).All(&data)

	return data, total
}

//新增
func InspectsSumCreate(insp *InspectsSum) (int64, error) {

	insp.CreatedAt = time.Now().Unix()
	insp.CreatedBy = CurLoginUserInfo.RealName
	insp.DepartmentId = CurLoginUserInfo.DepartmentId
	insp.UpdatedAt = time.Now().Unix()
	insp.CreatedBy = CurLoginUserInfo.RealName

	return orm.NewOrm().Insert(insp)
}

//修改
func InspectsSumUpdate(insp *InspectsSum, isPay bool) (int64, error) {

	to,_ := orm.NewOrm().Begin()
	insp.UpdatedAt = time.Now().Unix()
	insp.CreatedBy = CurLoginUserInfo.RealName
	fields := utils.NeedUpdateFields(insp, "CreatedAt", "CreatedBy", "DepartmentId")
	id, err := orm.NewOrm().Update(insp, fields...)
	if err != nil {
		to.Rollback()
		return 0, err
	}
	if isPay { //处理支付
		if _, err := to.QueryTable(InspectsTableName()).Filter("inspectssum_id", insp.Id).Update(orm.Params{"pay_status": 1}); err != nil {
			to.Rollback()
			return 0, err
		}
		//减去档案收费项
		if _, err := to.QueryTable(ArchivesTableName()).Filter("id", insp.Archives.Id).Update(orm.Params{"pay_frequency": orm.ColValue(orm.ColMinus, insp.ProNum)}); err != nil {
			to.Rollback()
			return 0, err
		}
	}
	to.Commit()
	return id, nil
}

//查询单条记录
func InspectsSumFindOne(id int64) *InspectsSum {
	data := new(InspectsSum)
	orm.NewOrm().QueryTable(InspectsSumTableName()).Filter("id", id).One(data)
	return data
}

//查询当日记录
func InspectsSumQueryNowDate(archives_id int64) *InspectsSum {
	data := new(InspectsSum)
	startDate := utils.GetDateParse(utils.GetDate(time.Now().Unix()))
	endDate := startDate + 86400
	orm.NewOrm().QueryTable(InspectsSumTableName()).Filter("archives_id", archives_id).Filter("payment_type", 2).Filter("created_at__gt", startDate).Filter("created_at__lt", endDate).One(data)
	return data
}
